package com.laikan.reader.core.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import java.util.ArrayList;

/**
 * @author 功能描述：ViewPager适配器，用来绑定数据和view
 */
public class ViewPagerAdapter extends PagerAdapter {

    //界面列表
    private ArrayList<View> views;

    public ViewPagerAdapter(ArrayList<View> views) {
        this.views = views;
    }

    /**
     * 获得当前界面数
     */
    @Override
    public int getCount() {
        if (views != null) {
            return Integer.MAX_VALUE;
        }
        return 0;
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        position %= views.size();
        View view = views.get(position);
        ViewParent vp = view.getParent();
        if (vp!=null){
             ViewGroup parent = (ViewGroup)vp;
             parent.removeView(view);
                   }
        container.addView(view);

        return view;
    }

    /**
     * 判断是否由对象生成界面
     */
    @Override
    public boolean isViewFromObject(View view, Object arg1) {
        return (view == arg1);
    }

    /**
     * 销毁position位置的界面
     */
    @Override
    public void destroyItem(View view, int position, Object arg2) {
//        ((ViewPager)view).removeView(views.get(position));

    }
}
